use "cleandata\winprob_matched_dataset180_300.dta" , clear

keep if !timecontrol_iv

replace mover_eval=mover_eval/100





preserve
drop if winprob==.
xtile binscatter_bin=mover_eval,n(50)
drop if binscatter_bin==.
collapse result2 winprob mover_eval, by(binscatter_bin)
	
line result2 winprob mover_eval, lpattern(dash solid) name(oppclock, replace) ytitle("Win Probability") ///
	xtitle("Stockfish Evaluation") legend(label(1 "Empirical Probability") label(2 "Value Function"))
graph export "exhibits/eval_fit.pdf", as(pdf) replace
restore

// binscatter result2 winprob mover_eval if !matedummy, ///
// 	line(connect) lcolor(dkgreen orange_red)  n(100) name(eval, replace) m(none none) ytitle("Win Probability") ///
// 	xtitle("Stockfish Evaluation") legend(label(1 "Empirical Probability") label(2 "Value Function"))
// graph export "eval_fit.pdf", as(pdf) replace



preserve
drop if winprob==.
xtile binscatter_bin=clock,n(50)
drop if binscatter_bin==.
collapse result2 winprob clock, by(binscatter_bin)
	
line result2 winprob clock, lpattern(dash solid) name(oppclock, replace) ytitle("Win Probability") ///
	xtitle("Clock") legend(label(1 "Empirical Probability") label(2 "Value Function"))
graph export "exhibits/clock_fit.pdf", as(pdf) replace
restore

//
// binscatter result2 winprob clock, line(connect) lcolor(dkgreen orange_red)  n(50) name(clock, replace) m(none none) ytitle("Win Probability") ///
// 	xtitle("Clock") legend(label(1 "Empirical Probability") label(2 "Value Function")) 
// graph export "clock_fit.pdf", as(pdf) replace
	
	
preserve
drop if winprob==.
xtile binscatter_bin=opponentclock,n(50)
drop if binscatter_bin==.
collapse result2 winprob opponentclock, by(binscatter_bin)
	
line result2 winprob opponentclock, lpattern(dash solid) name(oppclock, replace) ytitle("Win Probability") ///
	xtitle("Opponent's Clock") legend(label(1 "Empirical Probability") label(2 "Value Function"))
graph export "exhibits/opp_clock_fit.pdf", as(pdf) replace
restore

preserve
drop if winprob==.
gen winbin=round(winprob*100)
replace winbin=0 if winprob<0
replace winbin=100 if winprob>1 & winprob!=.
replace winbin=winbin+1

keep if winprob!=.
collapse result2 winprob, by(winbin)
line result2 winprob winprob, lpattern(dash solid) ,  name(winprob, replace) ytitle("Win Probability") xtitle("Value Function") legend(label(1 "Empirical Probability") label(2 "Value Function"))
graph export "exhibits/winprob_fit.pdf", as(pdf) replace
restore

hist winprob if abs(winprob-.5)<1, xtitle("Estimated Value Function") start(-.5) width(.05) fcolor(white) lcolor(forest_green)
graph rename winprob_hist, replace
graph export "exhibits/winprob_hist.pdf", as(pdf) replace


summ marginalvalueoftime, detail
local p99=r(p99)
hist marginalvalueoftime  if  marginalvalueoftime<=`p99', xtitle("Estimated Marginal Value of Time") start(-.02) width(.001) fcolor(white) lcolor(forest_green)
graph rename marginalvalue_hist, replace
graph export "exhibits/marginalvalueoftime_hist.pdf", as(pdf) replace



reg result2 winprob
est store baseline

reg result2 winprob piecetaken 
est store piecetaken

gen move_check=substr(move, -1,1)=="+"
gen move_queen=substr(move,1,1)=="Q"
gen move_rook=substr(move,1,1)=="R"
gen move_knight=substr(move,1,1)=="N"
gen move_bishop=substr(move,1,1)=="B"
gen move_king=substr(move,1,1)=="K" | substr(move,1,1)=="O"


gen whites_move=mod(turn,2)==1

reg result2 winprob move_check
est store check
reg result2 winprob move_king move_queen move_rook move_bishop move_knight
est store whichpiece



encode move, gen(movecode)

reghdfe result2 winprob, absorb(i.movecode) nocon
estadd local fe "Move Notation"
est store allinfo

reghdfe result2 winprob, absorb(i.piecetaken#i.clock_percentile#i.eval_percentile#i.clock_opp_percentile#i.elo_percentile) nocon
estadd local fe "Capture $\times$ Bins"
est sto piecetaken_interaction


reghdfe result2 winprob, absorb(i.move_check#i.clock_percentile#i.eval_percentile#i.clock_opp_percentile#i.elo_percentile) nocon
estadd local fe "Check $\times$ Bins"
est sto check_interaction

reghdfe result2 winprob, absorb(i.movecode#i.clock_percentile#i.eval_percentile#i.clock_opp_percentile#i.elo_percentile) nocon
estadd local fe "Notation $\times$ Bins"
est sto movecode_interaction


label var winprob "Value Function"
label var piecetaken "Capture"
label var move_check "Check"
label var result2 "Game Result"

esttab baseline piecetaken check allinfo piecetaken_interaction check_interaction movecode_interaction, ///
	r2(%9.5f) se label ar2(%9.5f) scalar("fe Fixed Effects" "df_a Num Fixed Effects")
esttab baseline piecetaken check allinfo piecetaken_interaction check_interaction movecode_interaction ///
	using "exhibits/value_function_table.tex", r2(%9.5f) se label ar2(%9.5f) scalar("fe Fixed Effects" ///
	"df_a Num of Non-Dropped Fixed Effects") tex replace nonotes

esttab baseline piecetaken check allinfo piecetaken_interaction check_interaction movecode_interaction ///
	using "exhibits/value_function_table_nostar.tex", r2(%9.5f) se label ar2(%9.5f) scalar("fe Fixed Effects" ///
	"df_a Num of Non-Dropped Fixed Effects") tex replace nonotes nostar


